import pandas as pd
from sklearn.ensemble import AdaBoostRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error
import pickle
import util

Xcols = ['eirp',
          'ap_from_ap_0_max_ant_rssi', 'ap_from_ap_0_mean_ant_rssi', 'ap_from_ap_0_sum_ant_rssi',
        # 'ap_from_ap_1_max_ant_rssi', 'ap_from_ap_1_mean_ant_rssi', 'ap_from_ap_1_sum_ant_rssi',
          'sta_from_ap_0_max_ant_rssi', 'sta_from_ap_0_mean_ant_rssi', 'sta_from_ap_0_sum_ant_rssi',
          'sta_from_ap_1_max_ant_rssi', 'sta_from_ap_1_mean_ant_rssi', 'sta_from_ap_1_sum_ant_rssi',
          'sta_to_ap_0_max_ant_rssi', 'sta_to_ap_0_mean_ant_rssi', 'sta_to_ap_0_sum_ant_rssi',
          'sta_to_ap_1_max_ant_rssi', 'sta_to_ap_1_mean_ant_rssi', 'sta_to_ap_1_sum_ant_rssi']

# 读取数据
df = pd.read_csv('training_merge_ReduceRssi.csv')
missing_values = df[Xcols].isnull().any(axis=1)
df = df[~missing_values]
df = df[df['mcs'] < 11]
print('data len', len(df))

# 分割特征和目标列
X = df[Xcols]
Y = df['mcs']

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.1, random_state=42)

# 创建AdaBoost回归模型
adaboost = AdaBoostRegressor()
adaboost.fit(X_train, y_train)

# 保存模型
with open('mcsReg.pkl', 'wb') as f:
    pickle.dump(adaboost, f)

util.getAdaboostImportance(adaboost, Xcols)

# 计算拆分的测试集的均方误差
y_pred = adaboost.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print("MSE:", mse)